package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kang.Vo.GroupVo;
import com.kang.commons.Result;
import com.kang.commons.util.IpUtil;
import com.kang.commons.util.RedisUtil;
import com.kang.bean.GroupS;
import com.kang.bean.Photo;
import com.kang.bean.Users;
import com.kang.commons.util.TokenUtil;
import com.kang.mapper.GroupMapper;
import com.kang.mapper.PhotoMapper;
import com.kang.service.GroupService;
import com.kang.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @program: photo_fawu
 * @description: 分组服务
 * @author: K.faWu
 * @create: 2021-10-27 11:24
 **/
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, GroupS> implements GroupService {

    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private PhotoMapper photoMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserService userService;

    @Override
    public List<GroupVo> getAll(HttpServletRequest request) {
        ArrayList<GroupVo> groupVos = new ArrayList<>();
        String token = request.getHeader("token");
        Long id = TokenUtil.getUserIdToToken(token);
        if (id == -1){
            Result.error("登录过期，请重新登录！");
        }
        QueryWrapper<GroupS> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        List<GroupS> list = groupMapper.selectList(queryWrapper);
        list.forEach(group -> groupVos.add(setCoverPath(group)));
        return groupVos;
    }

    private GroupVo setCoverPath(GroupS group){
        //每个相册获取封面
        GroupVo groupVo = new GroupVo();
        BeanUtils.copyProperties(group,groupVo);
        Long coverId = group.getCoverId();
        Photo photo = null;
        if(coverId == null){
            QueryWrapper<Photo> photoQueryWrapper = new QueryWrapper<>();
            photoQueryWrapper.eq("group_id", group.getId());
            List<Photo> photoList = photoMapper.findByGroupIdAndSortId(group.getId());
            if (photoList != null && photoList.size() > 0) {
                photo = photoList.get(0);
            }
        }else{
            photo = photoMapper.selectById(coverId);
        }
        if (photo !=null) {
            groupVo.setCoverPath(photo.getPath());
        }
        return groupVo;
    }

    @Override
    public GroupS add(GroupS group, HttpServletRequest request) {
        String ip = IpUtil.getIpAddr(request);
        Long userId = (Long)redisUtil.getStringObject(ip);
        if (userId == null){
            Result.error("您的登录已过期,请重新登录!");
        }
        group.setUserId(userId);
        groupMapper.insert(group);
        return group;
    }

    @Override
    public void deleteGroup(Long id) {
        groupMapper.deleteById(id);
    }

    @Override
    public List<GroupVo> getByUserId(Long userId) {
        ArrayList<GroupVo> groupVos = new ArrayList<>();
        QueryWrapper<GroupS> groupQueryWrapper = new QueryWrapper<>();
        groupQueryWrapper.eq("user_id", userId);
        groupQueryWrapper.in("type",1,3);
        List<GroupS> list = groupMapper.selectList(groupQueryWrapper);
        list.forEach(groupS -> {
            groupS.setPassword(null);
            groupVos.add(setCoverPath(groupS));
        });
        return groupVos;
    }

    @Override
    public void isById(Long id, Users users) {
        GroupS groupS = groupMapper.selectById(id);
        if (groupS ==null || !groupS.getUserId().toString().equals(users.getId().toString())){
            Result.error("该相册非登录用户所有");
        }

    }

    @Override
    public String getPassById(Long id, HttpServletRequest request) {
        Users users = userService.getUsers(request);
        GroupS groupS = groupMapper.selectById(id);
        if (!users.getId().equals(groupS.getUserId())){
            log.debug(users.getName()+users.getPhone()+"——该用户试图获取他人相册密码！");
            Result.error("请勿获取他人密码！");
        }
        return groupS.getPassword();
    }

    @Override
    public void update(GroupS groupS) {
        //判断相册是否为加密类型
        if(groupS.getType() == 3){
            if (groupS.getPassword() == null || Objects.equals(groupS.getPassword(), "") ||groupS.getPassword().length()>16){
                Result.error("未设密码或密码超出长度！");
            }
        }else {
            groupS.setPassword("");
        }
        groupMapper.updateById(groupS);
    }
}
